home *** CD-ROM | disk | FTP | other *** search
/ FM Towns: Free Software Collection 10 / FM Towns Free Software Collection 10.iso / ms_dos / tool / m1024 / m1024.c < prev    next >
C/C++ Source or Header  |  1995-02-12  |  3KB  |  184 lines

  1. /*
  2. **  M1024.C    by JOUJI
  3. **
  4. **   Ver.1.00  1994.7
  5. **   Ver.1.10  1994.12 プリントファイルの最初の一行を無視しないようにした。
  6. **
  7. **  使い方:m1024 { file_name | /parameter } ...
  8. **
  9. **  '/'で始まる文字列はパラメーターとして扱われる。"$xx"は16進データ。
  10. */
  11.  
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <jctype.h>
  15.  
  16. unsigned short _asm_p();
  17.  
  18. /* FMR&TOWNS BIOS */
  19. #define bios_asm(x) \
  20.     _asm_p("\n\tINT\t94H\n", 0x100, _asm_p, _asm_p, (char)(x))
  21.  
  22. void print_file(char *fname);
  23. void print_cc(char *cc);
  24.  
  25. static char line[256];
  26.  
  27. main(int argc,char *argv[])
  28. {
  29.     int i;
  30.  
  31.     if (argc<2) {
  32.         puts("Parameter error !\n");
  33.         exit(1);
  34.     }
  35.     for(i=1; i<argc; i++) {
  36.         if(argv[i][0] == '/') {
  37.             print_cc(argv[i]);
  38.         } else {
  39.             print_file(argv[i]);
  40.         }
  41.     }
  42. }
  43.  
  44. void bios_print(char c)
  45. {
  46.     char d;
  47.     union {
  48.         unsigned short w;
  49.         struct {
  50.             char l;
  51.             char h;
  52.         } b;
  53.     } ax;
  54.  
  55.     for(;;) {
  56.         ax.w = bios_asm(c);
  57.         if(!ax.b.h) return;
  58.         if(kbhit()) {
  59.             d = getch();
  60.             if(d == '\x1b' || d == '\x03') exit(2);
  61.         }
  62.     }
  63. }
  64.  
  65. char hex2char(char *p)
  66. {
  67.     char x, y, c;
  68.  
  69.     x=0;
  70.     c=toupper(*(p++));
  71.     x = (c>='0' && c<='9') ? c - '0' : c - '7';
  72.     c=toupper(*p);
  73.     y = (c>='0' && c<='9') ? c - '0' : c - '7';
  74.     x=(x << 4) + y;
  75.     return x;
  76. }
  77.  
  78. void PrintControl(void)
  79. {
  80.     char c, *p;
  81.     int  i;
  82.     FILE  *fq;
  83.  
  84.     if(line[0] == 0x03) {            /* ^C:コメント */
  85.         return;
  86.     } else if(line[0] == 0x05) {    /* ^E:16進データ */
  87.         for(p = line + 1; *p != '\n'; p += 2) {
  88.             c = hex2char(p);
  89.             bios_print(c);
  90.         }
  91.     } else if(line[0] == 0x06) {    /* ^F:ビット・イメージ・データ・ファイル */
  92.         for(i=1; i<255; i++) {
  93.             if(line[i] == '\n') {
  94.                 line[i] = '\0';
  95.                 break;
  96.             }
  97.         }
  98.         if( (fq=fopen(&line[1], "rb")) == NULL) {
  99.             puts("File not exist !\n");
  100.             return;
  101.         }
  102.         while((i=fgetc(fq)) != EOF)
  103.             bios_print(i);
  104.         fclose(fq);
  105.     } else if(line[0] == 0x0a) {    /* ^J:空行 */
  106.         bios_print(0x0d);
  107.         bios_print(0x0a);
  108.     } else {
  109.         for(i=0; i<255; i++) {
  110.             c = line[i];
  111.             if(c == '\n' || c == '\0')
  112.                 break;
  113.             bios_print(c);
  114.         }
  115.     }
  116. }
  117.  
  118. void PrintLine(void)
  119. {
  120.     char c, d;
  121.     int  i;
  122.     union {
  123.         unsigned short w;
  124.         struct {
  125.             char l;
  126.             char h;
  127.         } b;
  128.     } sjis, jis;
  129.  
  130.     for(i=0; i<255; i++) {
  131.         c = line[i];
  132.         if(c == '\n' || c == '\0')
  133.             break;
  134.         if( (c >= 0x81 && c <= 0x9f) || (c >= 0xe0 && c <= 0xff) ) {
  135.             d = line[++i];
  136.             if(d == '\n' || d == '\0') break;
  137.             sjis.b.h = c;
  138.             sjis.b.l = d;
  139.             jis.w = mstojis(sjis.w);
  140.             bios_print(jis.b.h);
  141.             bios_print(jis.b.l);
  142.         } else if(c >= 0x20) {
  143.             bios_print(0);
  144.             bios_print(c);
  145.         } else {
  146.             bios_print(c);
  147.         }
  148.     }
  149. }
  150.  
  151. void print_file(char *fname)
  152. {
  153.     FILE *fp;
  154.  
  155.     if( (fp=fopen(fname,"r")) == NULL) {
  156.         puts("File not exist !\n");
  157.         return;
  158.     }
  159. /*    fgets(line, 256, fp); */ /* ファイルの最初の一行無視 */
  160.     while( fgets(line, 256, fp) != NULL) {
  161.         if(line[0] <= 0x1f) {
  162.             PrintControl();
  163.         }
  164.         else {
  165.             PrintLine();
  166.         }
  167.     }
  168.     fclose(fp);
  169. }
  170.  
  171. void print_cc(char *p)
  172. {
  173.     char c;
  174.  
  175.     for( p++; *p != '\0'; p++ ) {
  176.         c = *p;
  177.         if( c == '$' ) {
  178.             c = hex2char(++p);
  179.             p++;
  180.         }
  181.         bios_print(c);
  182.     }
  183. }
  184.